home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / 4utils73.zip / HANDLEIN.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-01  |  6KB  |  220 lines

  1. UNIT HandleIniFile;
  2. {$I-}
  3. (* ----------------------------------------------------------------------
  4.    Part of 4DESC - A Simple 4DOS File Description Editor
  5.        and 4FF   - 4DOS File Finder
  6.  
  7.    (c) 1992, 1993 Copyright by David Frey,
  8.                                Urdorferstrasse 30
  9.                                8952 Schlieren ZH
  10.                                Switzerland
  11.  
  12.        Code created using Turbo Pascal 6.0 (c) Borland International 1990
  13.  
  14.    DISCLAIMER: This unit is freeware: you are allowed to use, copy
  15.                and change it free of charge, but you may not sell or hire
  16.                this part of 4DESC. The copyright remains in our hands.
  17.  
  18.                If you make any (considerable) changes to the source code,
  19.                please let us know. (send a copy or a listing).
  20.                We would like to see what you have done.
  21.  
  22.                We, David Frey and Tom Bowden, the authors, provide absolutely
  23.                no warranty of any kind. The user of this software takes the
  24.                entire risk of damages, failures, data losses or other
  25.                incidents.
  26.  
  27.    This unit handles the reading of settings stored in 4UTILS.INI.
  28.  
  29.    ----------------------------------------------------------------------- *)
  30.  
  31. INTERFACE
  32.  
  33. CONST INIFileExists : BOOLEAN = FALSE;
  34.  
  35. PROCEDURE ReadIniFile;
  36.  
  37. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  38. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  39. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  40. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  41.  
  42. IMPLEMENTATION USES Dos, StringDateHandling;
  43.  
  44. CONST INIFileName = '4UTILS.INI';
  45.       MaxLines    = 41;
  46.  
  47. VAR INIFile : TEXT;
  48.     INIPath : DirStr;
  49.     MaxLine : INTEGER;
  50.     Line    : ARRAY[1..MaxLines] OF STRING[48];
  51.  
  52.     Name    : NameStr;
  53.     Ext     : ExtStr;
  54.  
  55. PROCEDURE ReadIniFile;
  56.  
  57. VAR semicol : BYTE;
  58.     ReadLine: STRING;
  59.     s       : STRING;
  60.  
  61.  PROCEDURE SearchPath;
  62.   BEGIN
  63.    INIPath := GetEnv('PATH');
  64.    INIPath := FSearch(INIFileName,INIPath);
  65.    IF INIPath > '' THEN
  66.      BEGIN
  67.        {$I-}
  68.        Assign(INIFile,INIPath);
  69.        Reset(INIFile);
  70.        INIFileExists := (IOResult = 0);
  71.        {$I-}
  72.      END;
  73.   END;
  74.  
  75. BEGIN  (* ReadIniFile *)
  76.  (* Search strategy for 4UTILS.INI :
  77.       i) Directory where this application has started from.
  78.      ii) Environment variable 4UTILS
  79.     iii) Path                                                   *)
  80.  
  81.  FSplit(ParamStr(0),INIPath,Name,Ext);
  82.  {$I-}
  83.  Assign(INIFile,INIPath+INIFileName);
  84.  Reset(INIFile);
  85.  {$I+}
  86.  INIFileExists := (IOResult = 0);
  87.  IF NOT INIFileExists THEN
  88.   BEGIN
  89.    INIPath := GetEnv('4UTILS');
  90.    IF INIPath > '' THEN
  91.      BEGIN
  92.        IF INIPath[Length(INIPath)] <> '\' THEN INIPath := INIPath + '\';
  93.        {$I-}
  94.        Assign(INIFile,INIPath+INIFileName);
  95.        Reset(INIFile);
  96.        {$I+}
  97.        INIFileExists := (IOResult = 0);
  98.        IF NOT INIFileExists THEN SearchPath;
  99.      END
  100.    ELSE SearchPath;
  101.   END;
  102.  
  103.  IF INIFileExists THEN
  104.   BEGIN
  105.    MaxLine := 1;
  106.    WHILE NOT Eof(INIFile) AND (MaxLine <= MaxLines) DO
  107.     BEGIN
  108.      ReadLn(INIFile,ReadLine);
  109.      StripLeadingSpaces(ReadLine);
  110.  
  111.      IF (ReadLine[1] <> ';') THEN
  112.       BEGIN
  113.        semicol := Pos(';',ReadLine);
  114.        IF (semicol > 0) AND
  115.           (DownStr(Copy(ReadLine,1,10)) <> 'delimiters') THEN
  116.         ReadLine := Copy(ReadLine,1,semicol-1);
  117.  
  118.        IF Length(ReadLine) > 0 THEN
  119.         BEGIN
  120.          StripTrailingSpaces(ReadLine);
  121.          Line[MaxLine] := DownStr(ReadLine); INC(MaxLine);
  122.         END;
  123.       END; (* IF Line[1] <> ';' .. *)
  124.     END; (* WHILE *)
  125.  
  126.    IF NOT Eof(INIFile) THEN
  127.     BEGIN
  128.      WriteLn('WARNING: '''',INIFileName,'''' exceeding ',MaxLines,' lines!');
  129.      WriteLn('         Parts of your settings will be ignored.');
  130.     END;
  131.  
  132.    {$I-}
  133.    Close(INIFile);
  134.    {$I+}
  135.   END; (* INIFileExists .. *)
  136. END; (* ReadIniFile *)
  137.  
  138. FUNCTION  ReadSettingsString(Section,Name: STRING; Default: STRING): STRING;
  139.  
  140. VAR LineNr: BYTE;
  141.     eq    : BYTE;
  142.     s,res : STRING;
  143.  
  144. BEGIN
  145.  LineNr := 1;
  146.  Section := '['+DownStr(Section)+']';
  147.  WHILE (Line[LineNr] <> Section) AND (LineNr <= MaxLine) DO INC(LineNr);
  148.  IF Line[LineNr] = Section THEN
  149.   BEGIN
  150.    DownString(Name); res := ''; s := '';
  151.    REPEAT
  152.     s  := Line[LineNr]; eq := Pos('=',s);
  153.     IF eq > 0 THEN s := Copy(s,1,eq-1);
  154.     StripTrailingSpaces(s);
  155.     INC(LineNr);
  156.    UNTIL (s = Name) OR (LineNr > MaxLine);
  157.  
  158.    IF s = Name THEN
  159.     BEGIN
  160.      res := Copy(Line[LineNr-1],eq+1,255);
  161.      StripLeadingSpaces(res);
  162.     END
  163.    ELSE res := Default;
  164.   END
  165.  ELSE res:= Default;
  166.  ReadSettingsString := res;
  167. END;
  168.  
  169. FUNCTION  ReadSettingsChar(Section,Name: STRING; Default: CHAR): CHAR;
  170.  
  171. VAR s : STRING;
  172.  
  173. BEGIN
  174.  s := ReadSettingsString(Section,Name,Default);
  175.  ReadSettingsChar := s[1];
  176. END;
  177.  
  178. FUNCTION  ReadSettingsInt(Section,Name: STRING; Default: INTEGER): INTEGER;
  179.  
  180. VAR s  : STRING;
  181.     res: INTEGER;
  182.     v  : INTEGER;
  183.  
  184. BEGIN
  185.  Str(Default,s);
  186.  s := ReadSettingsString(Section,Name,s);
  187.  Val(s,v,res);
  188.  IF res > 0 THEN v := Default;
  189.  ReadSettingsInt := v;
  190. END;
  191.  
  192. FUNCTION  ReadSettingsColor(Section,Name: STRING; Default: INTEGER): BYTE;
  193.  
  194. CONST color : ARRAY[0..15] OF STRING[12] =
  195.               ('black'   ,'blue'        ,'green'     ,'cyan'     ,
  196.                'red'     ,'magenta'     ,'brown'     ,'lightgray',
  197.                'darkgray','lightblue'   ,'lightgreen','lightcyan',
  198.                'lightred','lightmagenta','yellow'    ,'white');
  199.  
  200. VAR s  : STRING;
  201.     c  : BYTE;
  202.  
  203. BEGIN
  204.  Str(Default,s);
  205.  s := ReadSettingsString(Section,Name,'');
  206.  
  207.  IF s > '' THEN
  208.   BEGIN
  209.    c := 0;
  210.    WHILE (color[c] <> s) AND (c<16) DO INC(c);
  211.    IF color[c] <> s THEN c := Default;
  212.   END
  213.  ELSE c := Default;
  214.  ReadSettingsColor := c;
  215. END;
  216.  
  217. BEGIN
  218.  ReadIniFile;
  219. END.
  220.